package com.ljy.my_study.leetcode.两数相加;

import java.util.ArrayList;
import java.util.List;

/** 
* @author James
* @date 2018年9月16日 
*/
public class TestMain {
	
	public static void main(String[] args) {
		int a=143;
		int b=123;
		System.out.println(change10To2(a));
		System.out.println(change10To2(b));
		System.out.println("---------------------------------");
		System.out.println(add(a,b));
		System.out.println("---------------------------------");
		System.out.println(a+b);
		System.out.println(change10To2(a+b));
//		System.out.println(add(14,15));0
//		System.out.println(add(16,17));
//		System.out.println(add(34,24));
		
	}
	
	
	public static int add(int a,int b) {
		int x=a^b;
		int y=a&b;
		if(y==0) {
			return x;
		}else {
			return add(x,y<<1);
		}
	}
	
	public static String change10To2(int a) {
		List<Integer> list=new ArrayList<>();
		int x=0;
		int y=0;
		while(true) {
			x=a%2;
			y=a/2;
			if(x==0&&y==0) {
				break;
			}
			if(a==1) {
				list.add(a);
				break;
			}
			list.add(x);
			a=y;
		}
		StringBuilder sb=new StringBuilder();
		for(int i=list.size()-1;i>=0;i--) {
			sb.append(list.get(i));
		}
		if(list.isEmpty()) {
			return "0";
		}
		return sb.toString();
	}
	
}
